package com.yong.ssj.service.order;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

import org.springframework.stereotype.Service;

import com.yong.ssj.db.dao.order.IOrderDao;
import com.yong.ssj.db.dto.cost.CostMethodDto;
import com.yong.ssj.db.dto.order.OrderDto;
import com.yong.ssj.db.dto.order.OrderOperateDto;
import com.yong.ssj.db.dto.order.ReturnApplyDto;
import com.yong.ssj.util.DateTimeUtils;
import com.yong.ssj.vo.order.OrderOperateVo;

@Service(value="OrderServiceImpl")
public class OrderServiceImpl implements IOrderService{
	
	@Resource(name="OrderDaoImpl")
	private IOrderDao orderDao;

	@Override
	public List<Map<String, Object>> queryBorrow() {//申请借款
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = orderDao.queryOrder("0");
		return list;
	}

	@Override
	public List<Map<String, Object>> queryBorrowed() {//已经放款
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = orderDao.queryOrder("1");
		return list;
	}

	@Override
	public List<Map<String, Object>> queryReturn() {//申请还款
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = orderDao.queryReturn("0");
		return list;
	}

	@Override
	public List<Map<String, Object>> queryReturned() {//已经还款
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = orderDao.queryReturn("1");
		return list;
	}

	@Override
	public OrderOperateDto orderOperate(long order_id) {
		// TODO Auto-generated method stub
		OrderOperateDto dto = orderDao.queryOrderOperate(order_id);
		return dto;
	}

	@Override
	public int updateBorrow(OrderOperateVo vo) {//放款
		// TODO Auto-generated method stub
		vo.setOperate_type("1");
        String dateDir = DateTimeUtils.formatDate(new Date());
		vo.setOperate_time(dateDir);
		orderDao.updateOrderBorrow(vo);
		orderDao.updateOrderStatusf("1", vo.getOrder_id(),vo.getCustomer_account_type());
		List<Map<String, Object>> list = orderDao.queryFangmoney(vo.getOrder_id());
		double rz_surplus_money = 0;
        double apply_money =  0;
        int yan = 0;
		if (list != null && !list.isEmpty()){
	        Map<String, Object> map = null;
	            map = list.get(0);
	            rz_surplus_money = (double)map.get("rz_surplus_money");
	            apply_money =  (double)map.get("apply_money");
	            yan = orderDao.updateHuanmoney(rz_surplus_money-apply_money,(int)(long)map.get("customer_id"));
	    }
		return yan;
	}

	@Override
	public int updateReturn(OrderOperateVo vo) {//还款
		// TODO Auto-generated method stub
		vo.setOperate_type("2");
        String dateDir = DateTimeUtils.formatDate(new Date());
		vo.setOperate_time(dateDir);
		orderDao.updateOrderBorrow(vo);//建立还款信息单据
		orderDao.updateReturnStatus("1", vo);//修改预约还款订单信息
		List<Map<String, Object>> huanList = orderDao.queryhuan(vo.getReturn_apply_id());//查询具体还款信息
        Map<String, Object> map = huanList.get(0);
        double rz_surplus_money = (double)map.get("rz_surplus_money");
        double apply_money = (double)map.get("apply_money");
        long cost_id = (long)map.get("cost_id");
        double principal = (double)map.get("principal");//剩余本金
        int yan = 0;
        if(map.get("return_kinds").equals("1")){
        	orderDao.updateOrderStatus("2", vo.getOrder_id(),0);//修改还款订单的信息
        	yan = orderDao.updateHuanmoney(rz_surplus_money+apply_money, (Long)map.get("customer_id"));//借款额度增加
        }else{
        	List<Map<String, Object>> list = orderDao.queryCost();
        	Map<String, Object> cost = null;
        	int cost_month =0;
        	while(!list.isEmpty()){
        		cost = list.get(0);
        		if((long)cost.get("cost_id")==cost_id){
        			cost_month = (int)cost.get("cost_month");
        			break;
        		}else{
        			list.remove(0);
        		}
        	}
        	principal = principal - apply_money/cost_month;
        	if(principal<1){
        		orderDao.updateOrderStatus("2", vo.getOrder_id(),0);//修改还款订单的信息
        		yan = orderDao.updateHuanmoney(rz_surplus_money+apply_money, (Long)map.get("customer_id"));//借款额度增加
        	}else{
        		yan = orderDao.updateOrderStatus("1", vo.getOrder_id(),principal);//修改还款订单的信息
        	}
        	
        }
		return yan;
	}

	@Override
	public List<Map<String, Object>> queryBorrowph(String customer_name) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = orderDao.queryBorrow(customer_name);
		return list;
	}

	@Override
	public List<Map<String, Object>> queryReturnph(String customer_name) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = orderDao.queryReturnph(customer_name);
		return list;
	}

	@Override
	public List<Map<String, Object>> qupinz(String order_id) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list =orderDao.queryBo(Long.parseLong(order_id));
		return list;
	}

	@Override
	public List<Map<String, Object>> qupinRe(String return_apply_id) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list =orderDao.queryRe(Long.parseLong(return_apply_id));
		return list;
	}

	@Override
	public List<Map<String, Object>> queryCost() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> dto = orderDao.queryCost();
		return dto;
	}

	@Override
	public int insertBorrow(double apply_money,String cost_id, long customer_id) {
		// TODO Auto-generated method stub
		return orderDao.insertBorrow(apply_money,Long.parseLong(cost_id),customer_id);
	}

	@Override
	public List<Map<String, Object>> queryborrowList(long customer_id) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list =orderDao.queryborrowList(customer_id);
		return list;
	}

	@Override
	public List<Map<String, Object>> queryborrowedList(long customer_id) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list =orderDao.queryborrowedList(customer_id);
		return list;
	}

	@Override
	public List<Map<String, Object>> queryhuanList(long customer_id) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list =orderDao.queryhuanList(customer_id);
		return list;
	}

	@Override
	public List<Map<String, Object>> querytiqianList(long order) {
		// TODO Auto-generated method stub
		return orderDao.querytiqianList(order);
	}

	@Override
	public int querytiqianedList(long order,double money) {
		// TODO Auto-generated method stub
		return orderDao.querytiqianedList(order,money);
	}

	@Override
	public void madeReturn() {
		// TODO Auto-generated method stub
		Calendar a = Calendar.getInstance();
		a.set(Calendar.DATE-1, 1);
		a.roll(Calendar.DATE-1, -1);
		int maxDate = a.get(Calendar.DATE);//前一个月的天数
		List<Map<String, Object>> list = orderDao.queryShould();
		Map<String, Object> map = null;
		double return_money = 0;//需还款金额
		double principal = 0;//剩余本金
		double cost_day_rate = 0;//日利率
		while(!list.isEmpty()){
			map = list.get(0);
			principal = (double)map.get("principal");
			cost_day_rate = (double)map.get("cost_day_rate");
			return_money = (double)map.get("apply_money")/(int)map.get("cost_month")+principal*cost_day_rate*365/12;
			orderDao.insertReturn((long)map.get("order_id"), return_money, "0", "0");
			list.remove(0);
		}
	}
}
